//
//  adHttWLController.m
//  Estate
//
//  Created by heyong on 16/6/12.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "adHttWLController.h"
#import "A_addWLCell.h"
#import "addBtnCell.h"

@interface adHttWLController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *_showKeyArr;
//    int _num;
}
@end

@implementation adHttWLController

- (NSMutableDictionary *)dataDic{
    if(_dataDic==nil){
        _dataDic=[NSMutableDictionary dictionary];
    }
    return _dataDic;
}

//懒加载
- (NSMutableArray *)dataArray{
    if(_dataArray==nil){
        _dataArray=[NSMutableArray array];
    }
    return  _dataArray;
}

- (TPKeyboardAvoidingTableView *)tableView{
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRECTS(0, 0, 375, 667)];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView=[[UIView alloc]init];
        [_tableView registerClass:[A_addWLCell class] forCellReuseIdentifier:@"addCell"];
        [_tableView registerClass:[addBtnCell class] forCellReuseIdentifier:@"addbtn"];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"旁站-回填土施工-物料信息";
    self.num=0;
    [self setData];
    [self.view addSubview:self.tableView];
    //能编辑
    if(self.isEdit){
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];

    }
    
}
- (void)clickSave{
    if(_site){
        NSMutableArray * passOnArr=[NSMutableArray array];
        for (int i=0; i<self.dataArray.count; i++) {
            //模型转字典
            NSDictionary *dic = [self.dataArray[i] yy_modelToJSONObject];
            [passOnArr addObject:dic];
        }
        NSMutableDictionary *dics=[NSMutableDictionary dictionary];

        for(int i=0;i<passOnArr.count;i++){
            NSMutableDictionary *tempDic=[NSMutableDictionary dictionary];
            NSDictionary *dic=[passOnArr objectAtIndex:i];
            [tempDic setObject:dic[@"gmd"] forKey:@"干密度"];
            [tempDic setObject:dic[@"hsl"] forKey:@"含水量"];
            [tempDic setObject:dic[@"hdqy"] forKey:htt_ys_4];
            [tempDic setObject:dic[@"is"] forKey:@"是否满足施工组织需要"];
            [tempDic setObject:dic[@"tzbs"] forKey:@"填筑步数"];
            [tempDic setObject:dic[@"xphd"] forKey:@"虚铺厚度"];
            [tempDic setObject:dic[@"ysd"] forKey:@"压实度"];
            [tempDic setObject:dic[@"yshd"] forKey:@"压实厚度"];
            [dics setObject:tempDic forKey:dic[@"number"]];
        }
        self.dataBlock(dics);
        
    }else{
        
        NSMutableArray * passOnArr=[NSMutableArray array];
        
        for (int i=0; i<self.dataArray.count; i++) {
            //模型转字典
            NSDictionary *dic = [self.dataArray[i] yy_modelToJSONObject];
            
            [passOnArr addObject:dic];
        }
        
        NSMutableDictionary *dics=[NSMutableDictionary dictionary];

        for(int i=0;i<passOnArr.count;i++){
            NSMutableDictionary *tempDic=[NSMutableDictionary dictionary];
            NSDictionary *dic=[passOnArr objectAtIndex:i];
            [tempDic setObject:dic[@"gmd"] forKey:@"干密度"];
            [tempDic setObject:dic[@"hdqy"] forKey:@"环刀取样数量及编号"];
            [tempDic setObject:dic[@"is"] forKey:@"是否满足施工组织需要"];
            [tempDic setObject:dic[@"tzbs"] forKey:@"填筑步数"];
            [tempDic setObject:dic[@"hsl"] forKey:@"含水量"];
            [tempDic setObject:dic[@"xphd"] forKey:@"虚铺厚度"];
            [tempDic setObject:dic[@"ysd"] forKey:@"压实度"];
            [tempDic setObject:dic[@"yshd"] forKey:@"压实厚度"];
            
            [dics setObject:tempDic forKey:dic[@"number"]];
        }
        
        self.dataBlock(dics);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setData{
    WEAKSELF
    //查看页
    if(_site){
        //部位空
        if(![_site.wl_fctt isEqual:@""]){
            _showKeyArr=[NSMutableArray array];
            //循环遍历、找到显示的视图。
            for(int i=1;i<=10;i++){
                NSString *key=[NSString stringWithFormat:@"序号%d是否显示",i];
                NSString *value=[_site.wl_fctt objectForKey:key];
                //存入所有要显示的key
                if([value isEqualToString:@"是"]){
                    [_showKeyArr addObject:[NSString stringWithFormat:@"序号%d",i]];
                }
            }
            //取到显示视图的值 根据显示的key
            for (NSString *key in _showKeyArr) {
                Site_03_01 *site=[Site_03_01 siteWithDict:_site.wl_fctt[key] AndNumber:key];
                self.num=(int)[key integerValue];
                [weakSelf.dataArray addObject:site];
            }
        }else{
        
        }
       
    }
    
}

//addDataDic 默认为@""
- (void)addDataDefaine:(long)l{
    
    NSDictionary *dic=@{htt_ys_1:@"",htt_ys_2:@"",htt_ys_3:@"",htt_ys_4:@"",htt_ys_5:@"",htt_ys_6:@"",htt_ys_7:@"",hnt_r_8:@"是"};
    
    Site_03_01 *site=[Site_03_01 siteWithDict:dic AndNumber:[NSString stringWithFormat:@"序号%li",l]];
    
    [self.dataArray addObject:site];
    
}


#pragma  mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //最后一行 添加添加按钮
    if(indexPath.row==self.dataArray.count){
        return 40*BIZ;
    }
    return 330*BIZ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //+1  添加按钮新增了一行
    if(self.isEdit){
        return self.dataArray.count+1;
    }else{
        return self.dataArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    typeof(self) __weak weakSelf = self;
    
    //显示添加按钮
    if(indexPath.row==self.dataArray.count){
        
        addBtnCell *cell=[tableView dequeueReusableCellWithIdentifier:@"addbtn" forIndexPath:indexPath];
        //动态添加视图
        cell.addBlock=^(){
            //当前行+1
            [weakSelf addDataDefaine:self.num+1];
            
            [weakSelf.tableView reloadData];
            // 滚动到新增指定的区域
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count-1 inSection:0]
                                  atScrollPosition:UITableViewScrollPositionTop animated:YES];
        };
        
        return cell;
    }else{
        
        A_addWLCell *cell=[tableView dequeueReusableCellWithIdentifier:@"addCell"];
        
        //第一行隐藏 删除按钮
        if(indexPath.row==0){
            cell.dleBtn.hidden=YES;
        }else{
            cell.dleBtn.hidden=NO;
        }
        
        //设置数据
        Site_03_01 *siteDetail=[self.dataArray objectAtIndex:indexPath.row];
        cell.site=siteDetail;
        //设置序号
        cell.titLab.text=siteDetail.number;
        
        //设置标题
        NSArray *numStr=[siteDetail.number componentsSeparatedByString:@"号"];
        self.num=[[numStr lastObject]integerValue];
        
        //设置数据
        cell.text1Block=^(NSString *str){
            siteDetail.tzbs=str;
        };
        
        cell.text2Block=^(NSString *str){
            siteDetail.xphd=str;
        };
        
        cell.text3Block=^(NSString *str){
            siteDetail.yshd=str;
        };
        
        cell.text4Block=^(NSString *str){
            siteDetail.hdqy=str;
        };
        
        cell.text5Block=^(NSString *str){
            siteDetail.hsl=str;
        };
        
        cell.text6Block=^(NSString *str){
            siteDetail.gmd=str;
        };
        
        cell.text7Block=^(NSString *str){
            siteDetail.ysd=str;
        };
        
        cell.text8Block=^(UISwitch *sender){
            siteDetail.is=sender.on==0?@"否":@"是";
        };
        
        //移除当前行数
        cell.deleteBlock=^(){
            
            [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
            
            [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
            
            [weakSelf.tableView reloadData];
            
        };
        
        return cell;
        
    }
}

#pragma mark scrollview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        [self.view endEditing:YES];
    }
}


@end
